﻿
using HbcServerProcess.Interfaces;
using HbcServerProcess.Models;
using IotContract.Dtos.Clients;
using IotContract.Dtos.Servers;
using IotContract.Dtos.Servers.Pushs;
using IotContract.Enums;
using IotContract.Interfaces;
using IotContract.Interfaces.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace HbcServerProcess.DataProcess;
/// <summary>
/// 扫码枪数据处理
/// </summary>
public class BcrDataProcess : IServerProcessHandle
{
    public string ProcessName { get; set; }= HbcServerConst.ProcessName;
    public IotEquipType ProcessType { get; set; } = IotEquipType.BCR;

    public event EventHandler<IotEquipWarnDto> WarnEvent;

    private readonly IServerMainManager _mainProcess;
    private readonly IProcessDataService _processDataService;
    public BcrDataProcess(IServerMainManager serverMainManager,IProcessDataService processDataService)
    {
        _mainProcess = serverMainManager;
        _processDataService = processDataService;
    }

    public async Task<bool> ParsingData()
    {
        // 获取数据
        var equips = await _mainProcess.GetEquips<PushBcrDto>(new IotContract.Models.Clients.ClientRequestCommand
        {
            RequestType = IotEquipType.BCR.ToString(),
            ProtobufType = IotProtobufType.HBC_BCR.ToString()
        });
        // 赋值,根据站台号UnitNum进行分组
        if (equips.Any())
        {
           var dic= equips.ToLookup(x => x.UnitNum).ToConcurrentDictionary(x => x.Key.To<int>(), y => y.ToList());
            _processDataService.Bcrs = dic;
        }
        return true;
    }

    public async Task<bool> ProcessData()
    {
        return true;
    }

    public async Task<bool> ProcessOrder()
    {
        return true;
    }

    public async Task<bool> ProcessWarn()
    {
        return true;
    }

    public bool Stop()
    {
        _processDataService.Bcrs.Clear();
        return true;
    }
}
